/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import _entity.RoleInfo;
import _entity.UserInfo;
import entity.Roles;
import entity.Users;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author HP
 */
@Stateless
public class RolesBean implements RolesBeanRemote {

    @EJB
    private UsersBeanRemote usersBean;
    @PersistenceUnit(unitName = "_asmPU")
    private EntityManagerFactory emf;

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public List<RoleInfo> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Roles.class));
        List<Roles> list = getEntityManager().createQuery(cq).getResultList();
        List<RoleInfo> result = new ArrayList<RoleInfo>();

        for (Roles c : list) {
            RoleInfo item = new RoleInfo();

            List<UserInfo> pList = new ArrayList<UserInfo>();
            for (Users p : c.getUsersList()) {
                UserInfo pItem = new UserInfo();
                pItem.setEmail(p.getEmail());
                pItem.setFirstName(p.getFirstName());
                pItem.setLastName(p.getLastName());
                pItem.setUserName(p.getUserName());
                pItem.setUserPass(p.getUserPass());
                pItem.setRolesList(result);

                pList.add(pItem);
            }

            item.setDescription(c.getDescription());
            item.setRoleName(c.getRoleName());
            item.setUsersList(pList);

            result.add(item);
        }

        return result;
    }

    @Override
    public List<RoleInfo> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Roles.class));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        List<Roles> list = q.getResultList();
        List<RoleInfo> result = new ArrayList<RoleInfo>();

        for (Roles c : list) {
            RoleInfo item = new RoleInfo();

            List<UserInfo> pList = new ArrayList<UserInfo>();
            for (Users p : c.getUsersList()) {
                UserInfo pItem = new UserInfo();
                pItem.setEmail(p.getEmail());
                pItem.setFirstName(p.getFirstName());
                pItem.setLastName(p.getLastName());
                pItem.setUserName(p.getUserName());
                pItem.setUserPass(p.getUserPass());
                pItem.setRolesList(result);

                pList.add(pItem);
            }

            item.setDescription(c.getDescription());
            item.setRoleName(c.getRoleName());
            item.setUsersList(pList);

            result.add(item);
        }

        return result;
    }

    @Override
    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<Roles> rt = cq.from(Roles.class);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    @Override
    public RoleInfo find(Object id) {
        Roles c = getEntityManager().find(Roles.class, id);
        if (c != null) {
            RoleInfo item = new RoleInfo();

            List<UserInfo> uList = new ArrayList<UserInfo>();
            for (Users p : c.getUsersList()) {
                UserInfo pItem = new UserInfo();
                pItem.setEmail(p.getEmail());
                pItem.setFirstName(p.getFirstName());
                pItem.setLastName(p.getLastName());
                pItem.setUserName(p.getUserName());
                pItem.setUserPass(p.getUserPass());

                List<RoleInfo> rList = new ArrayList<RoleInfo>();
                for(Roles r : p.getRolesList()){
                    RoleInfo aRole = new RoleInfo();
                    aRole.setUsersList(uList);
                    aRole.setRoleName(r.getRoleName());
                    aRole.setDescription(r.getDescription());

                    rList.add(aRole);
                }

                pItem.setRolesList(rList);

                uList.add(pItem);
            }

            item.setDescription(c.getDescription());
            item.setRoleName(c.getRoleName());
            item.setUsersList(uList);

            return item;
        }
        return null;
    }

    @Override
    public void edit(RoleInfo role) {
        Roles item = new Roles();

        List<Users> pList = new ArrayList<Users>();
        for (UserInfo p : role.getUsersList()) {
            Users pItem = new Users();
            pItem.setEmail(p.getEmail());
            pItem.setFirstName(p.getFirstName());
            pItem.setLastName(p.getLastName());
            pItem.setUserName(p.getUserName());
            pItem.setUserPass(p.getUserPass());
            pItem.setRolesList(usersBean.find_2(p.getUserName()).getRolesList());

            pList.add(pItem);
        }

        item.setDescription(role.getDescription());
        item.setRoleName(role.getRoleName());
        item.setUsersList(pList);

        getEntityManager().merge(item);
    }

    @Override
    public void create(RoleInfo role) {
        Roles item = new Roles();

        List<Users> pList = new ArrayList<Users>();
        for (UserInfo p : role.getUsersList()) {
            Users pItem = new Users();
            pItem.setEmail(p.getEmail());
            pItem.setFirstName(p.getFirstName());
            pItem.setLastName(p.getLastName());
            pItem.setUserName(p.getUserName());
            pItem.setUserPass(p.getUserPass());
            pItem.setRolesList(usersBean.find_2(p.getUserName()).getRolesList());

            pList.add(pItem);
        }

        item.setDescription(role.getDescription());
        item.setRoleName(role.getRoleName());
        item.setUsersList(pList);

        getEntityManager().persist(item);
    }

    @Override
    public void remove(RoleInfo role) {
        Roles item = new Roles();

        List<Users> pList = new ArrayList<Users>();
        for (UserInfo p : role.getUsersList()) {
            Users pItem = new Users();
            pItem.setEmail(p.getEmail());
            pItem.setFirstName(p.getFirstName());
            pItem.setLastName(p.getLastName());
            pItem.setUserName(p.getUserName());
            pItem.setUserPass(p.getUserPass());
            pItem.setRolesList(usersBean.find_2(p.getUserName()).getRolesList());

            pList.add(pItem);
        }

        item.setDescription(role.getDescription());
        item.setRoleName(role.getRoleName());
        item.setUsersList(pList);

        getEntityManager().remove(getEntityManager().merge(role));
    }

    @Override
    public boolean isUserInRole(String userName, String roleName) {
        RoleInfo role = find(roleName);
        if (role != null) {
            for (UserInfo u : role.getUsersList()) {
                if (u.getUserName().equals(userName)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Roles find_2(Object id) {
        return getEntityManager().find(Roles.class, id);
    }
   
}
